package com.xinfu.newproject.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.gson.Gson;
import com.xinfu.newproject.R;
import com.xinfu.newproject.adapter.complaint_adapter.ComplaintListAdapter;
import com.xinfu.newproject.base.BaseActivity;
import com.xinfu.newproject.bean.complaint.bean.ComplaintDetailBean;
import com.xinfu.newproject.bean.complaint.bean.ComplaintEvaluateBean;
import com.xinfu.newproject.bean.complaint.bean.ComplaintRecordBean;
import com.xinfu.newproject.bean.complaint.bean.SubmitComplaintBean;
import com.xinfu.newproject.bean.login.LoginBean;
import com.xinfu.newproject.presenter.ComplaintPresenter;
import com.xinfu.newproject.util.BToast;
import com.xinfu.newproject.util.SPUtils;
import com.xinfu.newproject.view.IView;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.disposables.Disposable;

import static com.xinfu.newproject.contants.Constant.LOGIN;

public class ComplaintFeedbackActivity extends BaseActivity implements IView.IComplaintView {

    @BindView(R.id.activity_complaint_feedback_rv)
    RecyclerView mRecyclerView;

    @BindView(R.id.activity_complaint_feedback_srl)
    SwipeRefreshLayout mSwipeRefreshLayout;

    private ComplaintListAdapter complaintListAdapter;

    private ComplaintPresenter complaintPresenter = new ComplaintPresenter(this);

    private View notDataView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_complaint_feedback);
        ButterKnife.bind(this);
        registerBack();
        setActivityTitle("投诉建议记录");
        initViewBind();
        initLisener();
    }

    private void initLisener() {


    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isLogin()) {
            complaintPresenter.complaintRecord();
        }
    }

    private void initViewBind() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        complaintListAdapter = new ComplaintListAdapter();
        mRecyclerView.setAdapter(complaintListAdapter);
        notDataView = getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) mRecyclerView.getParent(), false);
        notDataView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isLogin()) {
                    complaintPresenter.complaintRecord();
                }
            }
        });
        initRefreshLayout();
        mSwipeRefreshLayout.setRefreshing(true);
    }

    /**
     * 判断是否可以请求数据
     */
    String token, userId, userName;

    private boolean isLogin() {

        LoginBean.ResultBean resultBean;
        String responseIntent = (String) SPUtils.get(this, LOGIN, "0");
        if (!responseIntent.equals("0")) {
            resultBean = new Gson().fromJson(responseIntent, LoginBean.ResultBean.class);
            token = resultBean.getToken();
            userId = resultBean.getUserInfo().getId();
            userName = resultBean.getUserInfo().getUsername();
        } else {
            BToast.showText(this, "未登录");
            return false;
        }

        return true;
    }

    /**
     * 下拉刷新
     */
    private void initRefreshLayout() {
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (isLogin()) {
                    complaintPresenter.complaintRecord();
                }
            }
        });
    }

    @Override
    public String header() {
        return token;
    }

    @Override
    public String userId() {
        return userId;
    }

    @Override
    public String type() {
        return null;
    }

    @Override
    public String name() {
        return null;
    }

    @Override
    public String telephone() {
        return null;
    }

    @Override
    public String picture() {
        return null;
    }

    @Override
    public String content() {
        return null;
    }

    @Override
    public String projectId() {
        return null;
    }

    @Override
    public String complaintId() {
        return null;
    }

    @Override
    public String tenantId() {
        return null;
    }

    @Override
    public Integer status() {
        return null;
    }

    @Override
    public void showToast(String content) {

    }

    @Override
    public void closeDispose(Disposable disposable) {

    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void getMsg(SubmitComplaintBean submitComplaintBean) {

    }

    @Override
    public void getMsg(ComplaintRecordBean complaintRecordBean) {

        if (complaintRecordBean.getResult() != null) {
            if (complaintRecordBean.getResult().size() > 0) {
                complaintListAdapter.setNewData(complaintRecordBean.getResult());
            } else {
                complaintListAdapter.setEmptyView(notDataView);
            }
        } else {
            complaintListAdapter.setEmptyView(notDataView);
        }

        if (mSwipeRefreshLayout != null) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
    }

    @Override
    public void getMsg(ComplaintDetailBean complaintDetailBean) {

    }

    @Override
    public void getMsg(ComplaintEvaluateBean complaintEvaluateBean) {

    }
}
